/*
 * network_private.h
 *
 *  Created on: 16.06.2012
 *      Author: Sebastian
 *
 *  Interner Header
 *  Wird nur von den network modulen verwendet
 */

#ifndef NETWORK_PRIVATE_H_
#define NETWORK_PRIVATE_H_

/*------INCLUDES------*/
#include "network.h"

/*------DEFINES------*/

#define MSG_SIZE 30
#define DATA_SIZE 16
#define TYPE_COUNT 5
#define TIME_OUT 1000 //[ms]
#define TIME_OUT_BT 10000 //[ms]
#define MAX_TRIES 5
#define ERROR_COUNT 9

#define MSG_FIFO_SIZE 6

#define NW_TRUE (1)
#define NW_FALSE (0)
// "123:567:901:345678901234567890"
// "FRO:TO :TYP:MESSAGE[16].....\r\r"

/*------TYPEDEFS------*/
typedef enum{
	ACK,	//acknowledge
	REQ, 	//request
	TER,	//terminate
	PNG,	//ping
	ERR		//error
}msgType;

typedef enum{
	NO_ERR = 0,
	UNKNOWN,
	TIMEOUT,
	FORMAT,
	PATTERN,
	TYPE,
	TARGET,
	NO_ACK,
	SEND
}errType;

typedef struct{
	nw_teamType from;
	nw_teamType	to;
	msgType 	type;
	char		data[DATA_SIZE];
	char 		tries;
}message;

/*------NAMES------*/
static const char msgTypeNames[TYPE_COUNT][4] = {
		"ACK",
		"REQ",
		"TER",
		"PNG",
		"ERR"
};

static const char msgErrNames[ERROR_COUNT][DATA_SIZE] = {
		"no error",
		"unknown",
		"timeout",
		"format",
		"pattern",
		"wrong type",
		"wrong target",
		"wrong ACK",
		"send"
};


/*------FUNCTIONS------*/
int isBlank(char c);
int serialisation(message* msg, char* string);
int deserialisation(char* raw, message* msg);
int formatCheck(char* rawMsg);

int sendMsg(char* buffer);
int readMsg(char* buffer);

int checkAcknowledge(message* req, message* ack);
int makeAcknowledge(nw_teamType me, message* req, message* ack);
int sendError(nw_teamType me, nw_teamType to, int err);

int connect();

int FIFO_get_fill();
int FIFO_is_empty();
int FIFO_is_full();
void FIFO_write(user_msg* message);
int FIFO_read(user_msg* message);

#endif /* NETWORK_PRIVATE_H_ */
